package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.Queries;

/**
 * Servlet implementation class Balanse
 */
@WebServlet("/Chef_functions")
public class Chef_functions extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Queries q = new Queries();
	HttpSession s;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Chef_functions() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String balanse_f = (String)s.getAttribute("balanse_f");
		switch(balanse_f){
		case "donation":
			float amount = Float.parseFloat(request.getParameter("donation"));
			q.balanseUpdate("", amount, "Dotacja szefa");
			s.setAttribute("transaction", 1);
			response.sendRedirect("chef/donation.jsp");
			break;
		case "payment":
			float payment = Float.parseFloat(request.getParameter("payment"));
			String worker = request.getParameter("worker");
			q.balanseUpdate(worker, -payment, "wynagrodzenie");
			s.setAttribute("transaction", 1);
			response.sendRedirect("chef/make_payment.jsp");
			break;
		case "checkout":
			float checkout = Float.parseFloat(request.getParameter("checkout"));
			String title = request.getParameter("title");
			q.balanseUpdate("chef", -checkout, title);
			s.setAttribute("transaction", 1);
			response.sendRedirect("chef/checkout.jsp");
			break;
		case "promote":
			String user = request.getParameter("client");
			String priv = request.getParameter("type");
			String PESEL = request.getParameter("pesel");
			int type = 0;
			switch(priv){
			case "Klient":
				type = 0;
				break;
			case "Pracownik":
				type = 1;
				break;
			case "Administrator":
				type = 2;
				break;
			case "Szef":
				type = 3;
				break;
			}
			String[] user1 = user.split(" ");
			int ID = 0;
			if(PESEL != null) {
				if(PESEL.length() != 11){
					s.setAttribute("transaction", 3);
				}
			}
			else if(PESEL == null) {
				s.setAttribute("transaction", 4);
			}
			if(user1.length == 2) {
				ID = q.getID(user1[0], user1[1], PESEL);
				int row = q.Promote(ID, type);
				s.setAttribute("transaction", 1);
				s.setAttribute("success", row);
			}
			else {
				s.setAttribute("transaction", 2);
			}
			response.sendRedirect("chef/promote.jsp");
			break;
		}

	}

}
